﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.Management.Controllers
{
    public class ProductGroupListController : Controller
    {
        // Create an instance of user manage
        LeadExpress.Repository.ProductGroupManagementRepository repository = new LeadExpress.Repository.ProductGroupManagementRepository();

        //
        // GET: /Management/ProductGroupList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            ProductGroupManagementModel model = new ProductGroupManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        //
        // GET: /Management/ProductGroupList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            ProductGroupManagementModel model = new ProductGroupManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            ProductGroup model = new ProductGroup();

            model.Code = form["Code"].ToString();
            model.Name = form["Name"].ToString();
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

            repository.Create(model);

            return RedirectToAction("Index", "ProductGroupList");
        }

        //
        // GET: /Management/ProductGroupList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            ProductGroupManagementModel model = new ProductGroupManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.ProductGroupId = id;

            return View(model);
        }

        //
        // GET: /Management/ProductGroupList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            ProductGroup model = new ProductGroup();

            if (model != null)
            {
                model.ID = id;
                model.Code = form["Code"].ToString();
                model.Name = form["Name"].ToString();
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

                repository.Save(model);
            }

            return RedirectToAction("Index", "ProductGroupList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            ProductGroupManagementModel model = new ProductGroupManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }
    }
}
